SlideShare a Scribd company logo
Class No.36  Data Structures http://ecomputernotes.com
Example Hash Functions If the keys are integers then  key%T  is generally a good hash function, unless the data has some undesirable features. For example, if  T = 10  and all keys end in zeros, then  key%T = 0  for all keys.  In general, to avoid situations like this,  T  should be a prime number. http://ecomputernotes.com
Collision Suppose our hash function gave us the following values: hash("apple") = 5 hash("watermelon") = 3 hash("grapes") = 8 hash("cantaloupe") = 7 hash("kiwi") = 0 hash("strawberry") = 9 hash("mango") = 6 hash("banana") = 2 kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9 •  Now what? hash("honeydew") = 6 http://ecomputernotes.com
Collision When two values hash to the same array location, this is called a  collision Collisions are normally treated as “first come, first served”—the first value that hashes to the location gets it We have to find something to do with the second and subsequent values that hash to this same location.  http://ecomputernotes.com
Solution for Handling collisions Solution #1:  Search from there for an empty location Can stop searching when we find the value  or  an empty location. Search must be wrap-around at the end. http://ecomputernotes.com
Solution for Handling collisions Solution #2:  Use a second hash function ...and a third, and a fourth, and a fifth, ...  http://ecomputernotes.com
Solution for Handling collisions Solution #3:  Use the array location as the header of a linked list of values that hash to this location http://ecomputernotes.com
Solution 1: Open Addressing This approach of handling collisions is called  open addressing ; it is also known as  closed hashing . More formally, cells at  h 0 (x) ,  h 1 (x) ,  h 2 (x) , … are tried in succession where  h i (x) = (hash(x) + f(i)) mod TableSize ,  with  f(0) = 0 . The function,  f , is the collision resolution strategy. http://ecomputernotes.com
Linear Probing We use  f(i) = i , i.e.,  f  is a linear function of  i . Thus location(x) = (hash(x) + i) mod TableSize   The collision resolution strategy is called  linear probing  because it scans the array sequentially (with wrap around) in search of an empty cell. http://ecomputernotes.com
Linear Probing: insert Suppose we want to  add  seagull  to this hash table Also suppose: hashCode(“seagull”) = 143 table[143]  is not empty table[143] != seagull table[144]  is not empty table[144] != seagull table[145]  is empty Therefore, put  seagull  at location 145 robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull http://ecomputernotes.com
Linear Probing: insert Suppose you want to  add   hawk  to this hash table Also suppose hashCode(“hawk”) = 143 table[143]  is not empty table[143] != hawk table[144]  is not empty table[144] == hawk hawk  is already in the table, so do nothing. robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 . . . http://ecomputernotes.com
Linear Probing: insert Suppose: You want to add  cardinal  to this hash table hashCode(“cardinal”) = 147 The last location is 148 147 and 148 are occupied Solution: Treat the table as circular; after 148 comes 0 Hence,  cardinal  goes in location 0 (or 1, or 2, or ...) robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 http://ecomputernotes.com
Linear Probing: find Suppose we want to find  hawk  in this hash table We proceed as follows: hashCode(“hawk”) = 143 table[143]  is not empty table[143] != hawk table[144]  is not empty table[144] == hawk ( found! ) We use the same procedure for looking things up in the table as we do for inserting them robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 . . . http://ecomputernotes.com
Linear Probing and Deletion If an item is placed in array[hash(key)+4], then the item just before it is deleted How will probe determine that the “hole” does not indicate the item is not in the array? Have three states for each location Occupied Empty (never used) Deleted (previously used) http://ecomputernotes.com
Clustering One problem with linear probing technique is the tendency to form “clusters”. A cluster is a group of items not containing any open slots The bigger a cluster gets, the more likely it is that new values will hash into the cluster, and make it ever bigger. Clusters cause efficiency to degrade. http://ecomputernotes.com
Quadratic Probing Quadratic probing uses different formula: Use F(i) = i 2  to resolve collisions If hash function resolves to H and a search in cell H is inconclusive, try H + 1 2 , H + 2 2 , H + 3 2 , … Probe  array[hash(key)+1 2 ], then array[hash(key)+2 2 ], then array[hash(key)+3 2 ], and so on Virtually eliminates primary clusters http://ecomputernotes.com
Collision resolution: chaining Each table position is a linked list Add the keys and entries anywhere in the list (front easiest) 4 10 123 key entry key entry key entry key entry key entry No need to change position! http://ecomputernotes.com
Collision resolution: chaining Advantages over open addressing: Simpler insertion and removal Array size is not a limitation  Disadvantage Memory overhead is large if entries are small. 4 10 123 key entry key entry key entry key entry key entry http://ecomputernotes.com

More Related Content

PPTX
Open addressiing &rehashing,extendiblevhashing
PPT
Open Addressing on Hash Tables
PDF
Functional programming from its fundamentals
PDF
Streamlining the Inclusion of Computer Experiments in Research Papers
PPT
18 hashing
PDF
Home work II
PDF
The Chain Rule, Part 2
Open addressiing &rehashing,extendiblevhashing
Open Addressing on Hash Tables
Functional programming from its fundamentals
Streamlining the Inclusion of Computer Experiments in Research Papers
18 hashing
Home work II
The Chain Rule, Part 2

What's hot (20)

PDF
Python_ 3 CheatSheet
PDF
LabPal: Repeatable Computer Experiments Made Easy (ACM Workshop Talk)
PPT
Hashing
PPTX
Python programming for Beginners - II
PPTX
3.4 looking for real roots of real polynomials t
PDF
Jan. 12 Binomial Factor Theorm
PPTX
하스켈 프로그래밍 입문 4
PDF
Algebra 2 Unit 5 Lesson 2
PDF
Tuples in Python
PDF
Mementopython3 english
PDF
Zippers
PPTX
List in Python
ODT
Introduction to biopython
PDF
20170509 rand db_lesugent
PPTX
하스켈 프로그래밍 입문 3
PDF
Hashing Algorithm
PDF
Slides econ-lm
PPTX
Quadratic formula 2
PDF
atan2 brief explain
Python_ 3 CheatSheet
LabPal: Repeatable Computer Experiments Made Easy (ACM Workshop Talk)
Hashing
Python programming for Beginners - II
3.4 looking for real roots of real polynomials t
Jan. 12 Binomial Factor Theorm
하스켈 프로그래밍 입문 4
Algebra 2 Unit 5 Lesson 2
Tuples in Python
Mementopython3 english
Zippers
List in Python
Introduction to biopython
20170509 rand db_lesugent
하스켈 프로그래밍 입문 3
Hashing Algorithm
Slides econ-lm
Quadratic formula 2
atan2 brief explain
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 3
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 4
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 6
computer notes - Data Structures - 18
computer notes - Data Structures - 8
computer notes - Data Structures - 33
computer notes - Data Structures - 29
computer notes - Data Structures - 22
computer notes - Data Structures - 26
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 10
computer notes - Data Structures - 3
computer notes - Data Structures - 15
computer notes - Data Structures - 13
computer notes - Data Structures - 23
computer notes - Data Structures - 16
computer notes - Data Structures - 4
computer notes - Deleting a node
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Data Structures - 19
computer notes - Data Structures - 6
Ad

Similar to computer notes - Data Structures - 36 (20)

PPTX
Hashing a searching technique in data structures
PPTX
session 15 hashing.pptx
PPT
Hashing Techniques in Data Strucures and Algorithm
PPTX
Data Structures- Hashing
PPTX
Hashing using a different methods of technic
PPTX
hashing in data structure for engineering.pptx
PPTX
hashing in data structure for Btech .pptx
PPTX
hashing in data structure for Btech.pptx
PPTX
Hashing
PPTX
Hashing And Hashing Tables
PPTX
HASHING.ppt.pptx
PPTX
8. Hash table
PPT
Hash presentation
PPT
Hashing In Data Structure Download PPT i
PPT
Hashing PPT
PDF
Hashing components and its laws 2 types
PPTX
Introduction to Hash Tables | What is a HashTable in Algorithm
PPTX
HASHING IS NOT YASH IT IS HASH.pptx
PPTX
linear probing
PPTX
Hashing_Unit4.pptx Data Structures and Algos
Hashing a searching technique in data structures
session 15 hashing.pptx
Hashing Techniques in Data Strucures and Algorithm
Data Structures- Hashing
Hashing using a different methods of technic
hashing in data structure for engineering.pptx
hashing in data structure for Btech .pptx
hashing in data structure for Btech.pptx
Hashing
Hashing And Hashing Tables
HASHING.ppt.pptx
8. Hash table
Hash presentation
Hashing In Data Structure Download PPT i
Hashing PPT
Hashing components and its laws 2 types
Introduction to Hash Tables | What is a HashTable in Algorithm
HASHING IS NOT YASH IT IS HASH.pptx
linear probing
Hashing_Unit4.pptx Data Structures and Algos

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 31
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 5
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
PPT
computer notes - Data Structures - 38
computer notes - Data Structures - 30
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 31
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
computer notes - Data Structures - 5
Computer notes - Controlling User Access
Computer notes - Using SET Operator
computer notes - Data Structures - 38

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
MYSQL Presentation for SQL database connectivity
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
MYSQL Presentation for SQL database connectivity
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Cloud computing and distributed systems.
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

computer notes - Data Structures - 36

  • 1. Class No.36 Data Structures http://ecomputernotes.com
  • 2. Example Hash Functions If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features. For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys. In general, to avoid situations like this, T should be a prime number. http://ecomputernotes.com
  • 3. Collision Suppose our hash function gave us the following values: hash("apple") = 5 hash("watermelon") = 3 hash("grapes") = 8 hash("cantaloupe") = 7 hash("kiwi") = 0 hash("strawberry") = 9 hash("mango") = 6 hash("banana") = 2 kiwi banana watermelon apple mango cantaloupe grapes strawberry 0 1 2 3 4 5 6 7 8 9 • Now what? hash("honeydew") = 6 http://ecomputernotes.com
  • 4. Collision When two values hash to the same array location, this is called a collision Collisions are normally treated as “first come, first served”—the first value that hashes to the location gets it We have to find something to do with the second and subsequent values that hash to this same location. http://ecomputernotes.com
  • 5. Solution for Handling collisions Solution #1: Search from there for an empty location Can stop searching when we find the value or an empty location. Search must be wrap-around at the end. http://ecomputernotes.com
  • 6. Solution for Handling collisions Solution #2: Use a second hash function ...and a third, and a fourth, and a fifth, ... http://ecomputernotes.com
  • 7. Solution for Handling collisions Solution #3: Use the array location as the header of a linked list of values that hash to this location http://ecomputernotes.com
  • 8. Solution 1: Open Addressing This approach of handling collisions is called open addressing ; it is also known as closed hashing . More formally, cells at h 0 (x) , h 1 (x) , h 2 (x) , … are tried in succession where h i (x) = (hash(x) + f(i)) mod TableSize , with f(0) = 0 . The function, f , is the collision resolution strategy. http://ecomputernotes.com
  • 9. Linear Probing We use f(i) = i , i.e., f is a linear function of i . Thus location(x) = (hash(x) + i) mod TableSize The collision resolution strategy is called linear probing because it scans the array sequentially (with wrap around) in search of an empty cell. http://ecomputernotes.com
  • 10. Linear Probing: insert Suppose we want to add seagull to this hash table Also suppose: hashCode(“seagull”) = 143 table[143] is not empty table[143] != seagull table[144] is not empty table[144] != seagull table[145] is empty Therefore, put seagull at location 145 robin sparrow hawk bluejay owl . . . 141 142 143 144 145 146 147 148 . . . seagull http://ecomputernotes.com
  • 11. Linear Probing: insert Suppose you want to add hawk to this hash table Also suppose hashCode(“hawk”) = 143 table[143] is not empty table[143] != hawk table[144] is not empty table[144] == hawk hawk is already in the table, so do nothing. robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 . . . http://ecomputernotes.com
  • 12. Linear Probing: insert Suppose: You want to add cardinal to this hash table hashCode(“cardinal”) = 147 The last location is 148 147 and 148 are occupied Solution: Treat the table as circular; after 148 comes 0 Hence, cardinal goes in location 0 (or 1, or 2, or ...) robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 http://ecomputernotes.com
  • 13. Linear Probing: find Suppose we want to find hawk in this hash table We proceed as follows: hashCode(“hawk”) = 143 table[143] is not empty table[143] != hawk table[144] is not empty table[144] == hawk ( found! ) We use the same procedure for looking things up in the table as we do for inserting them robin sparrow hawk seagull bluejay owl . . . 141 142 143 144 145 146 147 148 . . . http://ecomputernotes.com
  • 14. Linear Probing and Deletion If an item is placed in array[hash(key)+4], then the item just before it is deleted How will probe determine that the “hole” does not indicate the item is not in the array? Have three states for each location Occupied Empty (never used) Deleted (previously used) http://ecomputernotes.com
  • 15. Clustering One problem with linear probing technique is the tendency to form “clusters”. A cluster is a group of items not containing any open slots The bigger a cluster gets, the more likely it is that new values will hash into the cluster, and make it ever bigger. Clusters cause efficiency to degrade. http://ecomputernotes.com
  • 16. Quadratic Probing Quadratic probing uses different formula: Use F(i) = i 2 to resolve collisions If hash function resolves to H and a search in cell H is inconclusive, try H + 1 2 , H + 2 2 , H + 3 2 , … Probe array[hash(key)+1 2 ], then array[hash(key)+2 2 ], then array[hash(key)+3 2 ], and so on Virtually eliminates primary clusters http://ecomputernotes.com
  • 17. Collision resolution: chaining Each table position is a linked list Add the keys and entries anywhere in the list (front easiest) 4 10 123 key entry key entry key entry key entry key entry No need to change position! http://ecomputernotes.com
  • 18. Collision resolution: chaining Advantages over open addressing: Simpler insertion and removal Array size is not a limitation Disadvantage Memory overhead is large if entries are small. 4 10 123 key entry key entry key entry key entry key entry http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 41. Start of lecture 42.
  • #19: End of Lecture 42.